package cn.com.assistant.service.impl;

import cn.com.assistant.entity.AnswerEntity;
import cn.com.assistant.entity.CasAnswerEntity;
import cn.com.assistant.entity.DataEntity;
import cn.com.assistant.entity.QueryEntity;
import cn.com.assistant.mapper.AnswerMapper;
import cn.com.assistant.mapper.CasAnswerMapper;
import cn.com.assistant.mapper.DataMapper;
import cn.com.assistant.service.AssistantService;
import cn.com.assistant.utils.ResponseMessage;
import cn.com.assistant.utils.UnitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.coyote.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service
@Slf4j
public class  AssistantServiceImpl implements AssistantService {

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private CasAnswerMapper casAnswerMapper;

    @Autowired
    private DataMapper dataMapper;


    @Override
    public String getToken() {
        try {
            return UnitService.getToken();
        } catch (Exception e) {
            return "token获取失败";
        }

    }

    @Override
    public Map<String, Object> query(QueryEntity queryEntity) {
        try {
            String result = UnitService.utterance(queryEntity);
            Map<String, Object> map = (Map<String, Object>) com.alibaba.fastjson.JSONObject.parse(result);
            Map<String, Object> result1 = (Map<String, Object>) map.get("result");
            String sessionId = String.valueOf(result1.get("session_id"));
            List<Map<String, Object>> responses = (List<Map<String, Object>>) result1.get("responses");
            Map<String, Object> map1 = responses.get(0);
            List<Map<String, Object>> actions = (List<Map<String, Object>>) map1.get("actions");
            Map<String, Object> map2 = actions.get(0);
            // 将返回的回答进行解析拿到结果集序号，再到知识库进行答案匹配
            String say = String.valueOf(map2.get("say"));
            String regEx="[^0-9]";
            Pattern p = Pattern. compile (regEx);
            Matcher m = p.matcher(say);
            String answerNum = m.replaceAll("").trim();
            List<Map<String, Object>> answerList;
            Map<String, Object> obj = new HashMap<>();
            obj.put("sessionId", sessionId);
            if (StringUtils.isNotEmpty(answerNum)) {
                // 知识库进行查询
                // 查询一级回复
                answerList = answerMapper.findMapByQId(answerNum);
                if (!CollectionUtils.isEmpty(answerList)) {
                    for (int i = 0; i < answerList.size(); i++) {
                        Map<String, Object> answerMap = answerList.get(i);
                        if (null != answerMap) {
                            // 查询二级回复
                            List<Map<String, Object>> casAnswerEntities = casAnswerMapper.findMapByPId(String.valueOf(answerMap.get("id")));
                            answerMap.put("casAnswerEntities", casAnswerEntities) ;

                        }
                    }
                }
                DataEntity dataEntity = dataMapper.findBySolutions(answerNum);
                if (null != dataEntity) {
                    obj.put("question", dataEntity.getQuestion());
                }
                obj.put("say", answerList);
                obj.put("code", 200);
            } else {
                // 没有正确回答时返回的答案
                List<Map<String, Object>> optionList = (List<Map<String, Object>>) map2.get("options");
                if (!CollectionUtils.isEmpty(optionList)) {
                    List<String> option = new ArrayList<>();
                    for (int i = 0; i < optionList.size(); i++) {
                        Map<String, Object> map3 = optionList.get(i);
                        String msg = String.valueOf(map3.get("option"));
                        option.add(msg);
                    }
                    obj.put("say", option);
                    obj.put("code", 300);
                }
            }
            return obj;
        } catch (Exception e) {
            log.info("访问出错");
        }
        return null;
    }

    @Override
    public ResponseMessage test() {
        List<AnswerEntity> answerEntities = answerMapper.findByQId("11");
        if (!CollectionUtils.isEmpty(answerEntities)) {
            for (int i = 0; i < answerEntities.size(); i++) {
                AnswerEntity answerEntity = answerEntities.get(i);
                if (null != answerEntity) {
                    // 查询二级回复.
                    List<CasAnswerEntity> casAnswerEntities = casAnswerMapper.findByPId(answerEntity.getId());
                    answerEntity.setCasAnswerEntities(casAnswerEntities);
                }
            }
        }
        return ResponseMessage.defaultResponse().setData(answerEntities);
    }
}
